class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sz=nums.size();
        int left=0;
        int right=0;
        int target=0;
        for(auto& a:nums)
            target+=a;
        target-=x;
        if(target<0)
            return -1;
        if(target==0)
            return sz;
        int count=-1;
        int mysum=0;
        while(right<sz)
        {
            mysum+=nums[right];
            while(mysum>target)
            {
                mysum-=nums[left];
                left++;
            }
            if(mysum==target)
                count=max(count,(right-left+1));
            right++;
            
        }
        if(count==-1)
            return -1;
        else
            return sz-count;
    }
};
